define('admin/js/mod/system/Role', [
    'utils/fetchEnhancer',
    'utils/helpers'], (fetch, helpers) => {

const Role = {
    data: function() {
        return {
            editDialogVisible: false,
            formData: {},
            list: null
        }
    },
    methods: {
        fetchList: function() {
            let _self = this;

            fetch('/adminapi/listrole').then(function(json){
                if(0 === json.status) {
                    _self.list = json.data.data;
                }
            });
        },
        handleTableClick: function(e) {
            let _self = this;
            let t = e.target;
            let role = t.getAttribute('data-role');
            let id = t.getAttribute('data-id');

            if('edit' === role) {
                fetch('/adminapi/getrole?id=' + id).then(function(json){
                    if(0 === json.status) {
                        _self.formData = json.data.data,
                        _self.editDialogVisible = true;
                    }
                });
                return;
            }

            if('delete' === role) {
                this.$confirm('确实要删除吗？', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    fetch('/adminapi/deleterole', {
                        method: 'POST',
                        body: 'id=' + id
                    }).then((json) => {
                        if(0 === json.status) {
                            _self.$message.success('删除成功');
                            _self.fetchList();
                        }
                    });

                }).catch(() => {});
            }
        },
        handleAddBtnClick: function() {
            this.formData = {};
            this.editDialogVisible = true;
        },
        doAddOrEdit: function() {
            let _self = this;
            let ret = helpers.json2Query(this.formData);
            let url = this.formData.id ? '/adminapi/editrole' : '/adminapi/addrole';

            fetch(url, {
                method: 'POST',
                body: ret
            }).then(function(json){
                if(0 === json.status) {
                    _self.formData = {},
                    _self.editDialogVisible = false;
                    _self.fetchList();

                    _self.$message.success('操作成功');
                } else {
                    _self.$message.error('操作失败');
                }
            });
        }
    },
    mounted: function() {
        this.fetchList();
    },
    template:
`<div>
    <el-button type="primary" size="medium" @click="handleAddBtnClick">新增导航</el-button>
    <table class="x-table" @click="handleTableClick">
        <tr>
            <th>id</th><th>名称</th><th>描述</th><th>操作</th>
        </tr>
        <template v-if="list && list.length">
            <tr v-for="item in list">
                <td>{{ item.id }}</td>
                <td>{{ item.role_name }}</td>
                <td>{{ item.role_describe }}</td>
                <td>
                    <span class="x-table-cursor-text" data-role="edit" :data-id="item.id">修改</span>
                    <span class="x-table-cursor-text" data-role="delete" :data-id="item.id">删除</span>
                </td>
            </tr>
        </template>
    </table>

    <el-dialog :title="formData.id ? '修改' : '新增'" :visible.sync="editDialogVisible">
        <el-form :model="formData">
            <el-form-item v-if="formData.id" label="id" label-width="100px">
                <el-input :disabled="true" v-model="formData.id"></el-input>
            </el-form-item>
            <el-form-item label="名称" label-width="100px">
                <el-input v-model="formData.role_name" maxlength="50" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="描述" label-width="100px">
                <el-input v-model="formData.role_describe" maxlength="50" autocomplete="off"></el-input>
            </el-form-item>
        </el-form>
        <div slot="footer">
            <el-button @click="editDialogVisible=false">取消</el-button>
            <el-button type="primary" @click="doAddOrEdit">确定</el-button>
        </div>
    </el-dialog>
</div>`
};

return Role;

});
